package com.jjyd.crlp.services.impl;

import com.jjyd.crlp.domain.entity.News;
import com.jjyd.crlp.domain.repository.NewsRepo;
import com.jjyd.crlp.model.dto.home.NewsInfoDto;
import com.jjyd.crlp.services.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

@Service
public class HomeServiceImpl implements HomeService {

    @Autowired
    private NewsRepo newsRepo;
    /**
     * 获取一定数量的新闻信息
     *
     * @param quantity 数量
     * @return 返回一定数量的新闻内容
     */
    @Override
    public List<NewsInfoDto> getNews(Integer quantity) {
        PageRequest pr = new PageRequest(0, quantity);
        List<News> newsList = newsRepo.findByRemoveFalseOrderByItemIdDesc(pr);
        List<NewsInfoDto> list = new LinkedList<>();
        for (News news : newsList) {
            NewsInfoDto newsInfoDto = new NewsInfoDto();
            newsInfoDto.setId(news.getId());
            newsInfoDto.setTitle(news.getTitle());
            newsInfoDto.setInfo(news.getInfo());
            String content = news.getContent();
            content = content.substring(0, content.length() - 4);

            newsInfoDto.setContent(content);
            list.add(newsInfoDto);
        }
        return list;
    }
}
